package com.xiyan.base.controller;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xiyan.base.entity.RolePermission;
import com.xiyan.base.service.IRolePermissionService;
import com.xiyan.base.utils.ResponseResult;
import com.xiyan.base.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ruiyanglee
 * @since 2020-07-23
 */
@RestController
@RequestMapping("/api/rolePermission")
@Api(value = "Role-Permission api", tags = "角色-权限接口")
public class RolePermissionController {
    private static final Logger log = LoggerFactory.getLogger(RolePermissionController.class);

    @Resource
    private IRolePermissionService rolePermissionService;

    @ApiOperation("角色权限信息列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult list() {
        try {
            List<RolePermission> rolePermissions = rolePermissionService.list();
            return ResponseResult.ok().data(rolePermissions);
        } catch (Exception e) {
            log.error("list interface exception info {}", e.getMessage());
            return ResponseResult.error().message("角色权限列表请求异常");
        }
    }

    @ApiOperation("角色权限新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseResult add(@RequestBody RolePermission rolePermission) {
        try {
            if (ObjectUtil.isNull(rolePermission)) {
                return ResponseResult.error().code(ResultCode.PARAMS_ERROR_CODE).message("role-permission参数为空");
            }
            rolePermissionService.saveOrUpdate(rolePermission);
            return ResponseResult.ok().message("角色权限新增成功");
        } catch (Exception e) {
            log.error("add interface exception info {}", e.getMessage());
            return ResponseResult.error().message("角色权限新增失败");
        }
    }

    @ApiOperation("角色权限信息展示")
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResponseResult findById(@PathVariable("id") String id) {
        try {
            if (StrUtil.isEmpty(id)) {
                return ResponseResult.error().code(ResultCode.PARAMS_ERROR_CODE).message("id为空");
            }
            RolePermission rolePermission = rolePermissionService.getById(id);
            return ResponseResult.ok().data(rolePermission);
        } catch (Exception e) {
            log.error("findById interface exception info {}", e.getMessage());
            return ResponseResult.error().message("角色权限信息请求异常");
        }
    }


    @ApiOperation("角色权限信息删除")
    @RequestMapping(value = "/del", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseResult del(@RequestParam String id) {
        try {
            if (StrUtil.isEmpty(id)) {
                return ResponseResult.error().code(ResultCode.PARAMS_ERROR_CODE).message("id为空");
            }
            rolePermissionService.removeById(id);
            return ResponseResult.ok().message("角色权限删除成功");
        } catch (Exception e) {
            log.error("del interface exception info {}", e.getMessage());
            return ResponseResult.error().message("角色权限删除失败");
        }
    }
}

